﻿using System;

namespace X_to_the_power_of_y
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var x = 2;
            var y = 5;

            Console.WriteLine(A(x, y, D));

            Console.Read();
        }
        //2147483648

        //二分?
        static double _sum = 1;
        static double D(double x, double y)
        {
            if (y / 2 < 2)
            {
                return C(x, y) * _sum;
            }
            if (y % 2 == 1)
            {
                _sum *= x;
                y -= 1;
            }
            return D(x * x, y / 2);
        }

        //递归
        static double C(double x, double y)
        {
            if (y <= 1)
            {
                return x;
            }
            return x * C(x, y - 1);
        }

        //循环
        static double B(double x, double y)
        {
            double sum = x;
            for (int i = 0; i < y - 1; i++)
            {
                sum = sum * x;
            }
            return sum;
        }


        static double A(double x, double y, Func<double, double, double> func)
        {
            if (y == 0)
            {
                return 0;
            }

            bool isNegative = false;

            if (y < 0)
            {
                isNegative = true;
                y = 0 - y;
            }

            double sum = func(x, y);

            if (isNegative)
            {
                sum = 1 / sum;
            }

            return sum;
        }
    }
}
